import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import Home from '../components/Home.vue'
import UserInfo from '../components/user/UserInfo.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    component: Login
  },
  {
    path: '/home',
    component: Home,
    children: [
      { path: '/user/userinfo', component: UserInfo }
    ]
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) => {
  if (to.path === "/login") {
    return next();
  }
  const token = window.sessionStorage.getItem("token");
  if (!token || token === 'undefined') {
    return next('/login');
  }
  return next();
})

export default router
